package com.objectoriented.tang.controller;


import com.objectoriented.tang.DBpojo.User;
import com.objectoriented.tang.WEBpojo.Result;
import com.objectoriented.tang.service.RequestUser;
import com.objectoriented.tang.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @CrossOrigin(value = "*")
    @RequestMapping("/test")
    public String test()
    {
        return "Sucessful!";
    }
    @RequestMapping("/all")
    public List<User> getAll(){
        return userService.getAllUser();
    }

    @CrossOrigin(value = "*")
    @RequestMapping("/user/login")
    public Result login(String id,String password){
        if(id==null || password == null)return Result.ofFail("1","参数格式错误");
        HashMap<String,Object> map= new HashMap<String,Object>();
        map.put("id",id);
        map.put("password",password);
        User result = userService.login(map);
        if(result!=null)return new Result().ofSuccess(result);
        else return Result.ofFail("2","用户名或密码错误");
    }

    @CrossOrigin(value = "*")
    @RequestMapping("/user/register")
    public Result register(String id, String name, String password, String image_url, String gender, String signature)
    {
        HashMap<String,Object> map= new HashMap<String,Object>();
        if(id==null || password == null || name==null)return Result.ofFail("1","参数格式错误");
        map.put("id",id);
        map.put("name",name);
        map.put("password",password);
        map.put("image_url",image_url);
        map.put("gender",gender);
        map.put("signature",signature);
        User result= userService.register(map);
        System.out.println(result);
        if(result!=null)return new Result().ofSuccess(result);
        else return Result.ofFail("1","该用户已存在");
    }

    @CrossOrigin(value = "*")
    @RequestMapping("/user/history")
    public Result getHistory(String player){
        if(player==null)return Result.ofFail("1","参数格式错误");
        int UID =Integer.parseInt(player);
        HashMap<String,Object> map= new HashMap<String,Object>();
        map.put("player",UID);
        return new Result().ofSuccess(userService.getHistory(map));
    }

    @CrossOrigin(value = "*")
    @RequestMapping("/user")
    public Result getUser(int UID){
        HashMap<String,Object> map= new HashMap<String,Object>();
        map.put("UID",UID);
        return new Result().ofSuccess(userService.getUser(map));
    }

    @CrossOrigin(value = "*")
    @RequestMapping("/user/ready")
    public Result match(String UID,String hidden_Point){
        if(UID==null || hidden_Point == null)return Result.ofFail("1","参数格式错误");
        RequestUser requestUser = new RequestUser(Integer.parseInt(UID),Integer.parseInt(hidden_Point),new Date());
        return new Result().ofSuccess(userService.ready(requestUser));
    }

    @CrossOrigin(value = "*")
    @RequestMapping("/addHistory")
    public Result addHistory(int user_A_UID, int user_B_UID, String startTime,String endTime,int winner)
    {
        HashMap<String,Object> map= new HashMap<String,Object>();
        map.put("user_A_UID",user_A_UID);
        map.put("user_B_UID",user_B_UID);
        map.put("startTime",startTime);
        map.put("endTime",endTime);
        map.put("winner",winner);
        System.out.println(startTime);
        return new Result().ofSuccess(userService.addHistory(map));
    }

    @CrossOrigin(value = "*")
    @RequestMapping("/user/update")
    public Result update(String value,String key,String UID)
    {
        if(value==null || key== null || UID==null)return Result.ofFail("1","参数格式错误");
        HashMap<String,Object> map= new HashMap<String,Object>();
        map.put("key",key);
        map.put("value",value);
        map.put("UID",UID);
        int code =userService.update(map);
        if(code==1)return new Result().ofSuccess(userService.update(map));
        else return Result.ofFail("1","修改信息失败");
    }

    @CrossOrigin(value = "*")
    @RequestMapping("/user/cancel")
    public Result cancel(String player)
    {
        if(player == null)return Result.ofFail("1","参数格式错误");
        return new Result().ofSuccess( userService.cancel(Integer.parseInt(player)));
    }
}
